package com.ruoyi.project.dict.hanyuzi.service.impl;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.dict.hanyuzi.domain.HanyuZi;
import com.ruoyi.project.dict.hanyuzi.mapper.HanyuZiMapper;
import com.ruoyi.project.dict.hanyuzi.service.IHanyuZiService;
import com.ruoyi.project.dict.zi.domain.Zi;
import com.ruoyi.project.dict.zi.service.IZiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 百度汉字Service业务层处理
 *
 * @author shixiangwen
 * @date 2021-01-29
 */
@Service
@DataSource(DataSourceType.DICT)
public class HanyuZiServiceImpl implements IHanyuZiService {

    @Autowired
    private HanyuZiMapper hanyuZiMapper;

    @Autowired
    private IZiService ziService;

    /**
     * 查询百度汉字
     *
     * @param id 百度汉字ID
     * @return 百度汉字
     */
    @Override
    public HanyuZi selectHanyuZiById(Long id) {
        return hanyuZiMapper.selectHanyuZiById(id);
    }

    /**
     * 查询百度汉字列表
     *
     * @param hanyuZi 百度汉字
     * @return 百度汉字
     */
    @Override
    public List<HanyuZi> selectHanyuZiList(HanyuZi hanyuZi) {
        return hanyuZiMapper.selectHanyuZiList(hanyuZi);
    }

    /**
     * 新增百度汉字
     *
     * @param hanyuZi 百度汉字
     * @return 结果
     */
    @Override
    public int insertHanyuZi(HanyuZi hanyuZi) {
        return hanyuZiMapper.insertHanyuZi(hanyuZi);
    }

    /**
     * 修改百度汉字
     *
     * @param hanyuZi 百度汉字
     * @return 结果
     */
    @Override
    public int updateHanyuZi(HanyuZi hanyuZi) {
        return hanyuZiMapper.updateHanyuZi(hanyuZi);
    }

    /**
     * 删除百度汉字对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteHanyuZiByIds(String ids) {
        return hanyuZiMapper.deleteHanyuZiByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除百度汉字信息
     *
     * @param id 百度汉字ID
     * @return 结果
     */
    @Override
    public int deleteHanyuZiById(Long id) {
        return hanyuZiMapper.deleteHanyuZiById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int impirt2zi(Long id) {

        HanyuZi hanyuZi = hanyuZiMapper.selectHanyuZiById(id);
        Zi query = new Zi();
        query.setZi(hanyuZi.getZi());
        List<Zi> zis = ziService.selectZiList(query);
        if (zis.size() <= 0) {
            return 0;
        }
        Zi zi = zis.get(0);
        boolean update = false;
        if (StringUtils.isEmpty(zi.getPinyin()) && StringUtils.isNotEmpty(hanyuZi.getPinyin())) {
            zi.setPinyin(hanyuZi.getPinyin());
            update = true;
        }
        if (StringUtils.isEmpty(zi.getXiangjie()) && StringUtils.isNotEmpty(hanyuZi.getBaike())) {
            //zi.setXiangjie(ZiUtil.format(hanyuZi.getPinyin(), hanyuZi.getBaike()));
            update = true;
        }

        if (update) {
            ziService.updateZi(zi);
        }

        hanyuZi.setStatus(1L);

        return hanyuZiMapper.updateHanyuZi(hanyuZi);

    }
}
